/*
 * @(#) CmAction .java  1.0  2004-4-12
 *
 * Copyright 2003 by LongTop Corporation.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * LongTop Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with LongTop.
 */
package com.cnc.ssm.web;

import com.cnc.bas.util.Constants;
import com.cnc.bas.util.LTUtil;
import com.cnc.bas.web.BasMapForm;
import com.cnc.bas.web.SecureBaseDispatchAction;
import com.cnc.ssm.ISsmService;
import com.cnc.ssm.model.PwEmployee;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
 

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * User: Du bf
 * Date: 2005-3-24
 */
public class EmployeeAction extends SecureBaseDispatchAction {
    private ISsmService ssmService = null;

    public void setServlet(ActionServlet actionServlet) {
        super.setServlet(actionServlet);
        ssmService = (ISsmService) this.getBean(Constants.BEAN_NAME_SSM);
    }

    public ActionForward search(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        request.setAttribute("listEmplType", ssmService.listCodeInfo(Constants.CODE_TYPE_0020));
        return mapping.findForward("search");
    }

    public ActionForward list(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HashMap hs = ((BasMapForm) form).getHs();
        LTUtil.addHsLike(hs, "emplCode,emplName");
        List list = ssmService.listEmployee(hs);
        LTUtil.setPageData(request, list);
        return mapping.findForward("list");
    }

    public ActionForward add(ActionMapping mapping,
                             ActionForm form,
                             HttpServletRequest request,
                             HttpServletResponse response) throws Exception {
        PwEmployee param = ((EmployeeForm) form).getEmployee();
        ssmService.addEmployee(param);
        setCodeAttribute(request);
        return mapping.findForward("edit");
    }

    public ActionForward update(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        PwEmployee param = ((EmployeeForm) form).getEmployee();
        ssmService.updateEmployee(param);
        setCodeAttribute(request);
        return mapping.findForward("edit");
    }

    public ActionForward delete(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        String emplCode = request.getParameter("id");
        ssmService.deleteEmployee(emplCode);
        return mapping.findForward("list");
    }

    public ActionForward edit(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        String emplCode = request.getParameter("id");
        PwEmployee obj = (PwEmployee) ssmService.loadEmployee(emplCode);
        ((EmployeeForm) form).setEmployee(obj);
        setCodeAttribute(request);
        return mapping.findForward("edit");
    }

    public ActionForward create(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        setCodeAttribute(request);
        return mapping.findForward("edit");
    }

    private void setCodeAttribute(HttpServletRequest request) {
        request.setAttribute("listEmplType",ssmService.listCodeInfo(Constants.CODE_TYPE_0020));
        request.setAttribute("listSex",     ssmService.listCodeInfo(Constants.CODE_TYPE_0001));
        request.setAttribute("listEduc",    ssmService.listCodeInfo(Constants.CODE_TYPE_0002));
    }
}